﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using SFS.SharePoint.DynamicForms;

namespace SFS.SharePoint.DynamicForms
{
    public class BaseDynamicWebpart : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SFS.SharePoint.DynamicForms/DynamicForm.ascx";

        protected override void CreateChildControls()
        {

            DynamicForm control = Page.LoadControl(_ascxPath) as DynamicForm;
            control.OnItemAdded += new DynamicForm.ItemAdded(OnItemAdded);
            control.OnItemAdding += new DynamicForm.ItemAdding(OnItemAdding);
            control.RenderingMode = DynamicForm.RenderMode.New;
            if (this.RenderingTemplate == "List")
                control.RenderingTemplate = DynamicForm.TemplateMode.List;
            else
                control.RenderingTemplate = DynamicForm.TemplateMode.Template;
            control.ListName = this.ListName;
            control.ShowFields = this.ShowFields;//"Title,EventDate,EndDate,Description,fAllDayEvent";
            control.FormTitle = this.FormTitle;
            control.TemplateID = this.TemplateID;
            control.TemplateIDColumn = this.TemplateIdColumn;
            control.TemplateDataColumn = this.TemplateDataColumn;
            control.SaveUrl = this.SaveUrl;
            control.CancelUrl = this.CancelUrl;
            control.DisplayContentType = this.DisplayContentType;
            control.IgnoreFields = this.IgnoreFields;
            control.ShowCancel = this.ShowCancel;
            control.SaveCaption = this.SaveCaption;
            control.CancelCaption = this.CancelCaption;
            control.SaveButtonWidth = this.SaveButtonWidth;
            control.CancelButtonWidth = this.CancelButtonWidth;
            control.CloseFormOnSubmit = this.CloseFormOnSubmit;
            control.TemplateSourceList = this.TemplateSourceList;
            control.ShowHeader = this.ShowFormHeader;
            Controls.Add(control);
        }

        public virtual void OnItemAdding(Microsoft.SharePoint.SPListItem item,DynamicFormEventArgs e)
        {

        }

        public virtual void OnItemAdded(Microsoft.SharePoint.SPListItem item)
        {

        }

        public void SaveChanges()
        {
            this.SetPersonalizationDirty();
        }


        public override object WebBrowsableObject
        {
            get { return this; }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            var editors = new List<EditorPart> { new PropertiesEditor { ID = this.ID + "_editorPart" } };
            return new EditorPartCollection(editors);
        }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string FormTitle { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string ListName { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string TemplateSourceList { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string TemplateID { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string TemplateIdColumn { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string TemplateDataColumn { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string SaveUrl { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string CancelUrl { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string RenderingMode { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string RenderingTemplate { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public bool DisplayContentType { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string IgnoreFields { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string ShowFields { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public bool ShowCancel { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string SaveCaption { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public string CancelCaption { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public int SaveButtonWidth { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public int CancelButtonWidth { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public bool CloseFormOnSubmit { get; set; }

        [WebPartStorage(Storage.Shared), Personalizable(PersonalizationScope.Shared)]
        public bool ShowFormHeader { get; set; }
    }
}
